﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace OrderManagement.Tests
{
    public class TestRunner
    {
        public static void RunAllTests()
        {
            var testResults = new List<string>();

            // 运行OrderService测试
            testResults.AddRange(RunOrderServiceTests());
            // 运行Order测试
            testResults.AddRange(RunOrderTests());
            // 运行OrderDetails测试
            testResults.AddRange(RunOrderDetailsTests());

            // 显示测试结果
            Console.WriteLine("\n=== 测试结果 ===");
            foreach (var result in testResults)
            {
                Console.WriteLine(result);
            }
            var passedCount = testResults.Count(r => r.Contains("[通过]"));
            var failedCount = testResults.Count(r => r.Contains("[失败]"));
            Console.WriteLine($"总测试数: {testResults.Count}, 通过: {passedCount}, 失败: {failedCount}");
        }

        private static List<string> RunOrderServiceTests()
        {
            var results = new List<string>();
            var service = new OrderService();

            try
            {
                // Test AddOrder_ValidOrder_Success
                var order = CreateSampleOrder("ORD001", "客户A");
                service.AddOrder(order);
                var result = service.QueryByOrderId("ORD001");
                if (result.Count == 1 && result[0].OrderId == "ORD001")
                    results.Add("[通过] AddOrder_ValidOrder_Success");
                else
                    results.Add("[失败] AddOrder_ValidOrder_Success");
            }
            catch (Exception ex)
            {
                results.Add($"[失败] AddOrder_ValidOrder_Success: {ex.Message}");
            }

            try
            {
                // Test AddOrder_DuplicateOrder_ThrowsException
                var order1 = CreateSampleOrder("ORD001", "客户A");
                var order2 = CreateSampleOrder("ORD001", "客户B");
                service.AddOrder(order1);
                try
                {
                    service.AddOrder(order2);
                    results.Add("[失败] AddOrder_DuplicateOrder_ThrowsException");
                }
                catch (ArgumentException)
                {
                    results.Add("[通过] AddOrder_DuplicateOrder_ThrowsException");
                }
            }
            catch (Exception ex)
            {
                results.Add($"[失败] AddOrder_DuplicateOrder_ThrowsException: {ex.Message}");
            }

            // 其他测试方法类似实现...

            return results;
        }

        private static List<string> RunOrderTests()
        {
            var results = new List<string>();

            try
            {
                // Test Order_Equals_ReturnsTrueForSameOrderId
                var order1 = new Order("ORD001", "客户A");
                var order2 = new Order("ORD001", "客户B");
                if (order1.Equals(order2))
                    results.Add("[通过] Order_Equals_ReturnsTrueForSameOrderId");
                else
                    results.Add("[失败] Order_Equals_ReturnsTrueForSameOrderId");
            }
            catch (Exception ex)
            {
                results.Add($"[失败] Order_Equals_ReturnsTrueForSameOrderId: {ex.Message}");
            }

            // 其他Order测试方法...

            return results;
        }

        private static List<string> RunOrderDetailsTests()
        {
            var results = new List<string>();

            try
            {
                // Test OrderDetails_Equals_ReturnsTrueForSameProductName
                var details1 = new OrderDetails("苹果", 10, 5);
                var details2 = new OrderDetails("苹果", 20, 6);
                if (details1.Equals(details2))
                    results.Add("[通过] OrderDetails_Equals_ReturnsTrueForSameProductName");
                else
                    results.Add("[失败] OrderDetails_Equals_ReturnsTrueForSameProductName");
            }
            catch (Exception ex)
            {
                results.Add($"[失败] OrderDetails_Equals_ReturnsTrueForSameProductName: {ex.Message}");
            }

            // 其他OrderDetails测试方法...

            return results;
        }

        private static Order CreateSampleOrder(string orderId, string customer)
        {
            return new Order(orderId, customer);
        }
    }
}